// @file: Provides some function for ajax communication with the server. The request event can be attached to form submit or to a link click.

//@param: init_function: a callback that will be called for initialization purposes, after the response data is embeded in the html code.
//@param: parameters: an object that will be passed to the init function.
function attachSubmitEvent(form, init_function, init_parameters, location_id){
	var form_id = $(form).attr("id");
	$(form).unbind('submit').bind('submit', function(event){
		var options = {
			success: function(data){
				//remove any elements with the id of the form.
				$(form).remove();
				responseHandler(data, init_function, init_parameters, location_id);
			}
		}
		//remove any ajax loader.
		$('#pager_ajax_loader').remove();		
		$(form).slideUp();
		$(this).ajaxSubmit(options);
		return false;
	});
}

function responseHandler(data, init_function, init_parameters, location_id){
                 if (location_id){
                     $("#"+location_id).html(data).fadeTo(500, 0.2).fadeTo(1500, 1);
                 } else {
                  $("body").append(data);
                 }		
		//call the init function if is defined.
		if (init_function){
			if (init_parameters){
				init_function(init_parameters);
			} else {
				init_function();
			}
		}
		//show and hide the textxs with js.
		if (window.show_divs){
			show_divs();
		}
		if (window.hide_divs){
			hide_divs();
		}
}